package com.foo.TextPreProcessing;

import java.util.regex.Pattern;

import cc.mallet.classify.Classification;
import cc.mallet.classify.NaiveBayes;
import cc.mallet.classify.NaiveBayesTrainer;
import cc.mallet.pipe.CharSequence2TokenSequence;
import cc.mallet.pipe.FeatureSequence2FeatureVector;
import cc.mallet.pipe.Pipe;
import cc.mallet.pipe.PrintInputAndTarget;
import cc.mallet.pipe.SerialPipes;
import cc.mallet.pipe.Target2Label;
import cc.mallet.pipe.TokenSequence2FeatureSequence;
import cc.mallet.pipe.iterator.ArrayIterator;
import cc.mallet.types.InstanceList;

public class MalletClassification {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//this is training string 1 for negative data
		String[] negativeTraining = new String[] {
				"profits disappoint","close low","loss","political unrest","war",
				"poor sales","disappointing","disappoint","actively seeking a buyer",
				"seeking","loan","default","plunge","fade","mortgage crisis","weak results",
				"misses estimates","shares slump","expenses soar","profits dropped","shares fall" ,
				"shares decline","financials drop","higher material cost","cuts forecast","sink",
				"profits sink","profit tumbles","tumbles","hurt profit","hurt","hit","merger set back",
				
				};
		
		//this is training string 2 for positive data
		String[] positiveTraining = new String[] {
				"widens","net income up","doubles profit","regains ground","surge","profitable",
				"jumps","soar","stock soars","blows past estimates","narrows laws",	"profit rises",
				"profit beats forecast","posts profit","revenues increase","high headed","ahead of the bell",
				"brightens on top","profit climbs","profit","increase","boost profit","profit hit record",
				"beats estimates","stands up and delivers","corporate expansion","overtake","acquire","acquires",
				"plans to take over","takes over","possible take over","raising bid","set to buy","jumps on deal",
				"demand acquire","confirm buyout","closes acquisation","may merge","startegic alliance",
				"M&A Call","bang"
				};
		Pattern tokenPattern = Pattern.compile("[\\p{L}\\p{N}_]+");
		InstanceList instances =
			new InstanceList (
					new SerialPipes (new Pipe[] {
							
							new Target2Label (),
							new CharSequence2TokenSequence (tokenPattern),
							new TokenSequence2FeatureSequence (),
							new FeatureSequence2FeatureVector (),
							 new PrintInputAndTarget()}));

		instances.addThruPipe (new ArrayIterator (negativeTraining, "negative"));
		instances.addThruPipe (new ArrayIterator (positiveTraining, "positive"));
		
		//classfier...training the classifier with these training instances...
		NaiveBayes c = new NaiveBayesTrainer ().train (instances);

		//this is test string!
		Classification cf = c.classify ("Google starts 2012 with the bang");
		System.out.println(cf.getLabeling().toString());
		System.out.println(c.getAccuracy(instances));
	}

}
